﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using School.Entities;
using System.Drawing;
namespace School.Pages.Bus
{
    public partial class Station : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1213) == true)
                {

                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                        ddlDistrict.Items.Insert(0, new ListItem("اختيار", "0"));
                        gvdistrictBind();
                    }
                    lbltestStation.Visible = false;
                    lbltestStationSearch.Visible = false;
                    if (!string.IsNullOrEmpty(Request.QueryString["type"]))
                        Bindcamp();
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        [System.Web.Services.WebMethod]
        public static string CheckCode(string args)
        {
            string returnValue = string.Empty;
            try
            {
                var qy = School.Service.Bus.GetStationByCode(args.Trim());
                if (qy.Count() == 0)
                    returnValue = "Available";
                else
                    returnValue = "<img src='../images/n.png'> <font color='#cc0000'><b>'" + args + "'</b> مستخدم.</font></img>";
            }
            catch
            { }
            return returnValue;

        }
        [System.Web.Services.WebMethod]
        public static string CheckName(string args)
        {

            string returnValue = string.Empty;
            try
            {
                var qy = School.Service.Bus.GetStationByName(args.Trim());
                if (qy.Count() == 0)
                    returnValue = "Available";
                else
                    returnValue = "<img src='../images/n.png'> <font color='#cc0000'><b>'" + args + "'</b> مستخدم.</font></img>";
            }
            catch
            { }
            return returnValue;

        }
        [System.Web.Services.WebMethod]
        private void Bindcamp()
        {
            string data = "";
            Response.Clear();
            Response.CacheControl = "no-cache";
            Response.ContentType = "application/json";
            System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
            data = json.Serialize(School.Service.Bus.GetAllCampuses().Select(x => new { x.Id, x.Name }));
            Response.Write(data);
            Response.End();
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }
        protected void btnSaveStation_Click(object sender, EventArgs e)
        {
            bool re = School.Service.Bus.AddStation(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlDistrict.SelectedValue.ToString()), txtScode.Text, txtSName.Text);
            if (re == true)
            {
                lbltestStation.ForeColor = Color.Green;

                lbltestStation.Text = "تمت الاضافة بنجاح";
                ddlBind(ddlDistrict, School.Common.Structure.GetAllDistrict().ToList());
                ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                gvdistrictBind();
                txtScode.Text = "";
                txtSName.Text = "";
            }
            else
            {
                lbltestStation.ForeColor = Color.Red;
                lbltestStation.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltestStation.Visible = true;
        }
        protected void gvdistrict_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvdistrict.PageIndex = e.NewPageIndex;
            gvdistrictBind();
        }

        protected void gvdistrict_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvdistrict.EditIndex = e.NewEditIndex;
            gvdistrictBind();
        }

        protected void gvdistrict_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvdistrict.EditIndex = -1;
            gvdistrictBind();
        }

        protected void gvdistrict_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvdistrict.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Service.Bus.DeleteStation(Id);
            if (re == true)
            {
                lbltestStationSearch.ForeColor = Color.Green;
                lbltestStationSearch.Text = "تم الحذف بنجاح";
                gvdistrictBind();
            }
            else
            {
                lbltestStationSearch.ForeColor = Color.Red;
                lbltestStationSearch.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltestStationSearch.Visible = true;
        }

        protected void gvdistrict_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvdistrict.DataKeys[e.RowIndex].Value.ToString());
            TextBox Name = (TextBox)gvdistrict.Rows[e.RowIndex].FindControl("txtSN");
            TextBox Code = (TextBox)gvdistrict.Rows[e.RowIndex].FindControl("txtSC");
            bool re = School.Service.Bus.UpdateStation(Id, Code.Text, Name.Text);
            if (re == true)
            {
                lbltestStationSearch.ForeColor = Color.Green;
                lbltestStationSearch.Text = "تم التحديث بنجاح";
                gvdistrict.EditIndex = -1;
                gvdistrictBind();
            }
            else
            {
                lbltestStationSearch.ForeColor = Color.Red;
                lbltestStationSearch.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestStationSearch.Visible = true;
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvdistrictBind();
        }

        private void gvdistrictBind()
        {
            int count;
            IEnumerable<SRV_Bus_Station> query = School.Service.Bus.GetStationtogrid();
            if (txtcodeSe.Text.Length > 0)
                query = query.Where(s => s.Sta_Code.Contains(txtcodeSe.Text));
            if (txtNamse.Text.Length > 0)
                query = query.Where(s => s.Name.Contains(txtNamse.Text));
            var gv = (from q in query
                      join Distr in db.LU_District
                          on q.District_Id equals Distr.Id
                      join camp in db.ORG_Campus on q.Campus_Id equals camp.Id
                      select new
                      {
                          camp.Name,
                          DistName = Distr.Name,
                          staName = q.Name,
                          q.Sta_Code,
                          q.District_Id,
                          q.Id
                      }).ToList();

            gvdistrict.DataSource = gv;
            gvdistrict.DataBind();
            count = gvdistrict.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + count.ToString();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            plsearch.Visible = false;
            Paneladd.Visible = true;
        }

        protected void btnexit_Click(object sender, EventArgs e)
        {
            txtcodeSe.Text = "";
            txtNamse.Text = "";
            plsearch.Visible = true;
            Paneladd.Visible = false;

        }

        protected void ddCamp1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (ddCamp1.SelectedIndex > 0)
                ddlBind(ddlDistrict, School.Common.Structure.GetDistrictByCamp(Convert.ToInt32(ddCamp1.SelectedValue)));
        }

        protected void btnexit_Click1(object sender, EventArgs e)
        {
            Paneladd.Visible = false;
            plsearch.Visible = true;
            txtScode.Text = "";
            txtSName.Text = "";

        }

        protected void btnNew_Click1(object sender, EventArgs e)
        {
            Paneladd.Visible = true;
            plsearch.Visible = false;
            txtcodeSe.Text = "";
            txtNamse.Text = "";
        }

        protected void gvdistrict_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }


    }

}